package com.yuexun.dhdoctor.module.my.ui;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/8/5.
 */
public class Act_EasyPassword extends BaseKJActivity {

    @Bind(R.id.pwd_one_img)
    ImageView pwdOneImg;
    @Bind(R.id.pwd_two_img)
    ImageView pwdTwoImg;
    @Bind(R.id.pwd_three_img)
    ImageView pwdThreeImg;
    @Bind(R.id.pwd_four_img)
    ImageView pwdFourImg;
    @Bind(R.id.pwd_edit_simple)
    EditText pwdEditSimple;
    @Bind(R.id.easypassword_tv_02)
    TextView easypassword_tv_02;

    StringBuilder builder;
    ImageView[] imageViews;
    String first_pwd, second_pwd;
    int times = 1;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_easypassword);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        builder = new StringBuilder();
        imageViews = new ImageView[]{pwdOneImg, pwdTwoImg, pwdThreeImg,
                pwdFourImg};
        pwdEditSimple.addTextChangedListener(mTextWatcher);
        pwdEditSimple.setOnKeyListener(keyListener);
    }

    TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() == 0) {
                return;
            }

            if (builder.length() < 4) {
                builder.append(s.toString());
                setTextValue();
            }
            s.delete(0, s.length());
        }
    };

    View.OnKeyListener keyListener = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_DEL
                    && event.getAction() == KeyEvent.ACTION_UP) {
                delTextValue();
                return true;
            }
            return false;
        }
    };

    private void setTextValue() {

        String str = builder.toString();
        int len = str.length();

        if (len <= 4) {
            imageViews[len - 1].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_circle));
        }
        if (len == 4) {
            if (times == 1) {
                first_pwd = str;
                imageViews[0].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                imageViews[1].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                imageViews[2].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                imageViews[3].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                easypassword_tv_02.setVisibility(View.VISIBLE);
                times = 2;
                builder.delete(0, len);
            } else if (times == 2) {
                second_pwd = str;
                if (first_pwd.equals(second_pwd)) {
                    ViewInject.toast("设置密码成功");
                    PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.ISPASSWORD, true);
                    PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.PASSWORD, first_pwd);
                    finish();
                } else {
                    ViewInject.toast("两次密码不一致，请重新输入");
                    times = 1;
                    imageViews[0].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                    imageViews[1].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                    imageViews[2].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                    imageViews[3].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
                    easypassword_tv_02.setVisibility(View.GONE);
                    builder.delete(0, len);
                }
            }
        }
    }

    private void delTextValue() {
        String str = builder.toString();
        int len = str.length();
        if (len == 0) {
            return;
        }
        if (len > 0 && len <= 4) {
            builder.delete(len - 1, len);
        }
        imageViews[len - 1].setImageDrawable(getResources().getDrawable(R.drawable.ic_password_line));
    }

    @OnClick({R.id.back, R.id.pwd_one_img, R.id.pwd_two_img, R.id.pwd_three_img, R.id.pwd_four_img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.pwd_one_img:
                break;
            case R.id.pwd_two_img:
                break;
            case R.id.pwd_three_img:
                break;
            case R.id.pwd_four_img:
                break;
        }
    }
}
